package com.meixiang.activity.homes.service;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.entity.services.ServiceDetail;
import com.meixiang.global.Config;
import com.meixiang.global.GlobalType;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.OldPriceView;
import com.meixiang.view.ProjectView;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 医美/医旅/基因检测详情
 * xiejingwen
 */
public class BeautyClinicDetailActivity extends BaseActivity {
    private String type="2"; //1 预约 2 医美 3 医旅 4 基因检测
    @Bind(R.id.iv_banner)
    ImageView ivBanner;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_now_price)
    TextView tvNowPrice;
    @Bind(R.id.ov_price)
    OldPriceView ovPrice;
    @Bind(R.id.tv_people_number)
    TextView tvPeopleNumber;
    @Bind(R.id.tv_introduce_content)
    TextView tvIntroduceContent;
    @Bind(R.id.tv_check_evaluate)
    TextView tvCheckEvaluate;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.iv_telephone)
    ImageView ivTelephone;
    @Bind(R.id.btn_reservation)
    Button btnReservation;
    @Bind(R.id.projiect_introduce)
    ProjectView projiectIntroduce;
    private ServiceDetail detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_beauty_clinic_detail);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        type=getIntent().getStringExtra("type");
        String checkText = "";
        if (type.equals("2")){
            checkText="查看医美评价";
            setTitle("医美详情");
        }else if (type.equals("3")){
            checkText="查看医旅评价";
            setTitle("医旅详情");
        }else if (type.equals("4")){
            checkText="查看基因检测评价";
            setTitle("基因检测详情");
        }else{
            setTitle("服务详情");
        }
        tvCheckEvaluate.setText(checkText);
    }

    @Override
    protected void setData() {
        getData();
    }

    private void getData() {
        status.showLoading();
        HttpParams params = new HttpParams();
        params.put("goodsId", getIntent().getStringExtra("goodsId"));
        HttpUtils.post(Config.SERVICE_DETAIL, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                status.removeView();
                detail = AbJsonUtil.fromJson(result.toString(), ServiceDetail.class);
                Glide.with(activity)
                        .load(detail.getGoodsImage())
                        .fitCenter()
                        .placeholder(R.drawable.image_default_gray_bg)
                        .error(R.drawable.image_default_gray_bg)
                        .into(ivBanner);
                final ArrayList<String> urls=new ArrayList<>();
                urls.add(detail.getGoodsImage());
                ivBanner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(activity, ShowPictureActivity.class);
                        intent.putStringArrayListExtra("urls", urls);
                        startActivity(intent);
                    }
                });
                tvTitle.setText(detail.getGoodsName());
                if (!AbStrUtil.isEmpty(detail.getGoodsStorePrice())) {
                    if (Double.parseDouble(detail.getGoodsStorePrice())>=1){
                        tvNowPrice.setText((int) Double.parseDouble(detail.getGoodsStorePrice())+"");
                    }else{
                        tvNowPrice.setText(detail.getGoodsStorePrice());
                    }
                } else {
                    tvNowPrice.setText("0");
                }
                if (!AbStrUtil.isEmpty(detail.getGoodsMarketPrice())) {
                    if (Double.parseDouble(detail.getGoodsStorePrice())>=1){
                        ovPrice.setText("¥ "+(int) Double.parseDouble(detail.getGoodsMarketPrice()) + "");
                    }else{
                        ovPrice.setText("¥ "+detail.getGoodsMarketPrice());
                    }
                } else {
                    ovPrice.setText("¥ 0");
                }

                tvPeopleNumber.setText(detail.getSalenum() + "人购买");
                if (AbStrUtil.isEmpty(detail.getGoodsBody())) {
                    tvIntroduceContent.setText("暂无描述");
                } else {
                    tvIntroduceContent.setText(detail.getGoodsBody());
                }
                if (AbStrUtil.isEmpty(detail.getAddress())) {
                    tvAddress.setText("暂无地址信息");
                } else {
                    tvAddress.setText(detail.getAddress());
                }
                projiectIntroduce.setData(detail.getInstructionList());

            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
                status.showNoData(message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getData();
                    }
                });
            }

        });
    }


    @OnClick({R.id.tv_check_evaluate, R.id.iv_telephone, R.id.btn_reservation})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
            case R.id.tv_check_evaluate:
                Bundle evaluate=new Bundle();
                evaluate.putString("o2oServiceId",detail.getGoodsId());
                evaluate.putString("type", GlobalType.SERVICE_EVALUATE_TYPE);
                startActivity(evaluate,ServiceEvaluateActivity.class);
                break;
            case R.id.iv_telephone:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + detail.getPhone()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.btn_reservation:
                Bundle bundle=new Bundle();
                bundle.putString("type",GlobalType.ADD_RESERVATION);
                bundle.putString("goodsId",detail.getGoodsId());
                bundle.putString("price",detail.getGoodsStorePrice());
                bundle.putString("isMainStore",detail.getIsMainStore());
                bundle.putString("goodsBody", detail.getGoodsName());
                bundle.putString("goodsImage", detail.getGoodsImage());
                startActivity(bundle,ReservationActivity.class);
                break;
        }
    }

}
